﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using G.Navigate;
using TianYa.API.Response;
using TianYa.API.Request;
using TianYa.Data;
using G;

namespace TianYa.View
{
    public partial class PostContenPage : GBasePage
    {
        public PostContenPage()
        {
            InitializeComponent();
        }
        BrowserRecorde BrowserRecorde;
        string baseUrl;
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            if (BrowserRecorde == null)
            {

                string plateid = this.NavigationContext.QueryString["plateid"];
                string postid = this.NavigationContext.QueryString["postid"];
                string name = this.NavigationContext.QueryString["name"];
                BrowserRecorde = GloabalData.Instance.TianYaDataBase.BrowserRecordeTable.FirstOrDefault(c => c.PlateId == plateid && c.PostId == postid);
                if (BrowserRecorde == null)
                {
                    BrowserRecorde = new Data.BrowserRecorde() { PageNum = 1, PlateId = plateid, PostId = postid, Name = name };
                    GloabalData.Instance.TianYaDataBase.BrowserRecordeTable.InsertOnSubmit(BrowserRecorde);
                }
                baseUrl = string.Format("http://3g.tianya.cn/bbs/art.jsp?item={0}&id={1}", plateid, postid);
                web.Navigate(new Uri(baseUrl + "&p=" + BrowserRecorde.PageNum + "&idwriter=0&key=0&chk="));
            }
        }
        protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedFrom(e);
            BrowserRecorde.TimeStamp = DateTime.Now.GetTimestamp();
            GloabalData.Instance.TianYaDataBase.SubmitChanges();
        }

        private void web_Navigating(object sender, NavigatingEventArgs e)
        {

            //if (!e.Uri.OriginalString.StartsWith(baseUrl))
            //{
            //    e.Cancel = true;
            //}
            //else
            //{
            var query = ParseQuesrString(e.Uri.Query.TrimStart('?'));
            if (query.ContainsKey("item") && query["item"] == BrowserRecorde.PlateId && query.ContainsKey("id") && query["id"] == BrowserRecorde.PostId)
            {
                if (query.ContainsKey("p"))
                {
                    int pagenum = BrowserRecorde.PageNum;
                    int.TryParse(query["p"], out pagenum);
                    BrowserRecorde.PageNum = pagenum;
                }
                this.RegisterLoading("正在加载....");
            }
            else
                e.Cancel = true;

            //}
        }
        private Dictionary<string, string> ParseQuesrString(string str)
        {
            Dictionary<string, string> dic = new Dictionary<string, string>();
            foreach (var item in str.Split('&'))
            {
                string[] kv = item.Split('=');
                if (kv.Length == 2)
                {
                    dic.Add(kv[0], kv[1]);
                }
            }
            return dic;
        }

        private void web_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
        {
            this.UnregisterLoading();
        }

        private void btnFavirate_Click(object sender, EventArgs e)
        {
            var fa = GloabalData.Instance.TianYaDataBase.FavoiratTable.FirstOrDefault(c => c.PlateId == BrowserRecorde.PlateId && c.PostId == BrowserRecorde.PostId);
            if (fa == null)
            {
                GloabalData.Instance.TianYaDataBase.FavoiratTable.InsertOnSubmit(new Favoirat() { Name = BrowserRecorde.Name, PlateId = BrowserRecorde.PlateId, PostId = BrowserRecorde.PostId });
                GloabalData.Instance.TianYaDataBase.SubmitChanges();
            }
            this.ShowState("收藏成功！");
        }

        private void menuOpenInSystem_Click(object sender, EventArgs e)
        {
            Microsoft.Phone.Tasks.WebBrowserTask w = new Microsoft.Phone.Tasks.WebBrowserTask();
            w.Uri = web.Source;
            w.Show();
        }
    }
}